package com.xx.doublexchat.common.user.dao;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.xx.doublexchat.common.user.domain.entity.UserApply;
import com.xx.doublexchat.common.user.domain.enums.ApplyReadStatusEnum;
import com.xx.doublexchat.common.user.domain.enums.ApplyStatusEnum;
import com.xx.doublexchat.common.user.domain.enums.ApplyTypeEnum;
import com.xx.doublexchat.common.user.mapper.UserApplyMapper;
import com.xx.doublexchat.common.user.service.IUserApplyService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 * 用户申请表 服务实现类
 * </p>
 *
 * @author xiaoXuan
 * @since 2024-02-21
 */
@Service
public class UserApplyDao extends ServiceImpl<UserApplyMapper, UserApply> {

    /**
     * 判断用户申请是否存在
     *
     * @param uid
     * @param targetUid
     * @return
     */
    public boolean validateApplySelf(Long uid, Long targetUid) {
        return lambdaQuery().eq(UserApply::getUid, uid)
                .eq(UserApply::getTargetId, targetUid)
                .eq(UserApply::getStatus, ApplyStatusEnum.WAIT_APPROVAL)
                .eq(UserApply::getType, ApplyTypeEnum.ADD_FRIEND.getCode())
                .count() > 0;
    }

    /**
     * 通过用户id和好友id查询申请记录
     *
     * @param targetUid
     * @param uid
     * @return
     */
    public UserApply getFriendApproving(Long targetUid, Long uid) {
        return lambdaQuery().eq(UserApply::getUid, uid)
                .eq(UserApply::getTargetId, targetUid)
                .eq(UserApply::getStatus, ApplyStatusEnum.WAIT_APPROVAL)
                .eq(UserApply::getType, ApplyTypeEnum.ADD_FRIEND.getCode())
                .one();
    }

    /**
     * 同意好友添加请求
     *
     * @param applyId
     */
    public void agree(Long applyId) {
        lambdaUpdate().set(UserApply::getStatus, ApplyStatusEnum.AGREE.getCode())
                .eq(UserApply::getId, applyId)
                .update();
    }

    /**
     * 获取该用户好友申请列表
     *
     * @param uid
     * @param plusPage
     * @return
     */
    public IPage<UserApply> friendApplyPage(Long uid, Page plusPage) {
        return lambdaQuery()
                .eq(UserApply::getTargetId, uid)
                .eq(UserApply::getType, ApplyTypeEnum.ADD_FRIEND.getCode())
                .orderByDesc(UserApply::getCreateTime)
                .page(plusPage);
    }

    /**
     * 更新阅读状态
     *
     * @param uid
     * @param applyIdList
     */
    public void readApples(Long uid, List<Long> applyIdList) {
        lambdaUpdate()
                .set(UserApply::getReadStatus, ApplyReadStatusEnum.READ.getCode())
                .eq(UserApply::getTargetId, uid)
                .eq(UserApply::getStatus,ApplyReadStatusEnum.UNREAD.getCode())
                .in(UserApply::getId,applyIdList)
                .update();
    }

    /**
     * 申请好友未读数
     * @param uid
     * @return
     */
    public Integer getUnReadCount(Long uid) {
        return lambdaQuery()
                .eq(UserApply::getTargetId,uid)
                .eq(UserApply::getStatus,ApplyReadStatusEnum.UNREAD.getCode())
                .count();
    }
}
